package middles

import (
	"github.com/gin-gonic/gin"
	systemControllers "golang-manage-admin/http/modules/system/controllers"
	"golang-manage-admin/providers"
)

// CheckIp
// @Description:  IP 验证
// @Author yang 2025-11-26 17:37:08
// @Return gin.HandlerFunc
func CheckIp() gin.HandlerFunc {
	controller := systemControllers.Controller{}
	return func(context *gin.Context) {
		clientIp := context.ClientIP()
		isAccess := false
		for _, ip := range providers.Config.GetStringSlice("whitelist.ip") {
			if ip == clientIp {
				isAccess = true
			}
		}
		if isAccess == true {
			context.Next()
		} else {
			controller.BadResponse(10501, clientIp, context)
			// 此处结束
			context.Abort()
			return
		}
	}
}
